<template>
	<el-container class="home-container">
		<!-- 头部 -->
		<el-header>
			<div>
				<img src="../assets/home-img.jpg" alt="" />
				<span>电商管理系统</span>
			</div>
			<el-button type="info" @click="logout">退出</el-button>
		</el-header>
		<!-- 主体 -->
		<el-container>
			<!-- 侧边栏-->
			<el-aside :width="isCollapse ? '4rem' : '12.5rem'">
				<div class="toggle-btn" @click="toggleCollapse">| | |</div>
				<!-- 侧边栏菜单区 -->
				<el-menu
					background-color="#4B5364"
					text-color="#fff"
					active-text-color="#ffd04b"
					unique-opened
					:collapse="isCollapse"
					:collapse-transition="false"
					router
					:default-active="activePath"
				>
					<!-- 一级菜单-->
					<el-submenu :index="'/' + item.path" v-for="item in menuList" :key="item.id">
						<!-- 一级菜单模板-->
						<template slot="title">
							<i :class="iconList[item.id]"></i>
							<span>{{ item.authName }}</span>
						</template>
						<!-- 二级菜单 -->
						<el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/' + subitem.path)">
							<template slot="title">
								<i class="el-icon-menu"></i>
								<span>{{ subitem.authName }}</span>
							</template>
						</el-menu-item>
					</el-submenu>
				</el-menu>
			</el-aside>
			<!-- 右边内容 -->
			<el-main><router-view></router-view></el-main>
		</el-container>
	</el-container>
</template>

<script>
export default {
	data() {
		return {
			menuList: [],
			iconList: {
				'125': 'iconfont icon-user',
				'103': 'iconfont icon-tijikongjian',
				'101': 'iconfont icon-shangpin',
				'102': 'iconfont icon-danju',
				'145': 'iconfont icon-baobiao'
			},
			isCollapse: false,
			activePath: ''
		};
	},
	created() {
		this.getMenuList();
		this.activePath = window.sessionStorage.getItem('activePath');
	},
	methods: {
		logout() {
			window.sessionStorage.clear();
			this.$router.push('/login');
		},
		async getMenuList() {
			const { data: res } = await this.$http.get('menus');
			console.log(res);
			if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
			this.menuList = res.data;
		},
		toggleCollapse() {
			this.isCollapse = !this.isCollapse;
		},
		saveNavState(ap) {
			window.sessionStorage.setItem('activePath', ap);
			this.activePath = ap;
		}
	}
};
</script>

<style lang="less" scoped>
.home-container {
	height: 100%;
}
.el-header {
	display: flex;
	justify-content: space-between;
	padding-left: 0;
	background: #282c35;
	color: #fff;
	font-size: 1.25rem;

	> div {
		display: flex;
		align-items: center;
	}
}
.el-aside {
	background: #4b5364;
	.el-menu {
		border-right: none;
	}
}
.el-main {
	background: #eee;
}
.iconfont {
	margin-right: 0.625rem;
}
.toggle-btn {
	background: #2c2f37;
	text-align: center;
	color: #fff;
	line-height: 1.5rem;
	font-size: 0.625rem;
	cursor: pointer;
}
</style>
